package com.maomao.admin.service.system.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.maomao.admin.common.utils.StringUtils;
import com.maomao.admin.domain.core.vo.TableDataInfo;
import com.maomao.admin.domain.system.entry.QSysNotice;
import com.maomao.admin.domain.system.entry.SysNotice;
import com.maomao.admin.persistence.repository.SysDictDataRepository;
import com.maomao.admin.persistence.repository.SysNoticeRepository;
import com.maomao.admin.service.system.AbstractPageService;
import com.maomao.admin.service.system.ISysNoticeService;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl extends AbstractPageService implements ISysNoticeService {

    @Autowired
    private SysNoticeRepository noticeMapper;

    @Resource
    private JPAQueryFactory queryFactory;


    private JPAQuery<SysNotice> getNoticeQuery(SysNotice notice) {
        JPAQuery<SysNotice> sysNoticeJPAQuery = queryFactory.selectFrom(QSysNotice.sysNotice);
        List<Predicate> predicates = new ArrayList<>();
        if (StringUtils.isNotBlank(notice.getNoticeTitle())) {
            predicates.add(QSysNotice.sysNotice.noticeTitle.contains(notice.getNoticeTitle()));
        }

        if (StringUtils.isNotBlank(notice.getNoticeType())) {
            predicates.add(QSysNotice.sysNotice.noticeType.contains(notice.getNoticeType()));
        }
        if (StringUtils.isNotBlank(notice.getCreateBy())) {
            predicates.add(QSysNotice.sysNotice.createBy.contains(notice.getCreateBy()));
        }
        if (predicates.size() > 0) {
            sysNoticeJPAQuery.where(predicates.toArray(new Predicate[0]));
        }
        return sysNoticeJPAQuery;
    }

    @Override
    public TableDataInfo<SysNotice> selectNoticePage(SysNotice notice) {
        JPAQuery<SysNotice> jpaQuery = this.getNoticeQuery(notice);
        super.startPage(jpaQuery);
        return new TableDataInfo<>(jpaQuery.fetch(), jpaQuery.fetchCount());
    }

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId) {
        return noticeMapper.findById(noticeId).orElse(null);
    }


    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice) {
        JPAQuery<SysNotice> jpaQuery = this.getNoticeQuery(notice);
        return jpaQuery.fetch();
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int insertNotice(SysNotice notice) {
        noticeMapper.save(notice);
        return 1;
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    public int updateNotice(SysNotice notice) {
        noticeMapper.save(notice);
        return 1;
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeById(Long noticeId) {
        noticeMapper.deleteById(noticeId);
        return 1;
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    public int deleteNoticeByIds(Long[] noticeIds) {
        noticeMapper.deleteAllById(Arrays.asList(noticeIds));
        return 1;
    }
}
